package com.taiyi.renaissancemall.Utils;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;


import androidx.annotation.Nullable;

import com.taiyi.renaissancemall.R;

import java.util.Locale;


/**
 * author: pearce on 2019-11-12
 * version: v1.0.0
 */
public class Utils {
    private static Context mContext;
    private static SPUtils mSpUtils;
    public static final int zh_cn = 0;
    public static final int zh_tw = 1;
    public static final int en_us = 2;
    //    public static final String SYSTEM_LOCALE = "system_locale";
    public static final String CUSTOM_LOCALE = "custom_locale";

    public static final int CHINESE = 1;
    public static final int ENGLISH = 2;

    /**
     * 初始化工具类
     * 初始化SP Utils，sp的name为当前应用的ApplicationId加后缀".preference"
     *
     * @param context 当前应用的Context
     */
    public static void init(Context context) {
        Utils.mContext = context;
        mSpUtils = new SPUtils(mContext, context.getApplicationInfo().processName + ".preference");
    }

    /**
     * 获取当前应用的Application Context
     *
     * @return ApplicationContext context
     */
    public static Context getContext() {
        if (mContext != null) {
            return mContext;
        }
        throw new NullPointerException("应该首先初始化");
    }

    /**
     * 获取SP utils对象
     *
     * @return sp utils 对象
     */
    public static SPUtils getSpUtils() {
        return mSpUtils;
    }

//    public static boolean getPermissions(List<PermissionItem> permissonItems, String msg) {
//        final Boolean[] isGetPermissions = {false};
//        HiPermission.create(mContext)
//                .permissions(permissonItems)
//                .title(mContext.getResources().getString(R.string.dear_user))
//                .msg(msg)
//                .animStyle(R.style.PermissionAnimScale)
//                .style(R.style.PermissionThemeStyle)
//                .checkMutiPermission(new PermissionCallback() {
//                    @Override
//                    public void onClose() {
//                        isGetPermissions[0] = false;
//                        ToastUtils.showShortToast(mContext.getResources().getString(R.string.close_permisson_toast));
//                    }
//
//                    @Override
//                    public void onFinish() {
//                        isGetPermissions[0] = true;
//                    }
//
//                    @Override
//                    public void onDeny(String permisson, int position) {
//                    }
//
//                    @Override
//                    public void onGuarantee(String permisson, int position) {
//                    }
//                });
//        return isGetPermissions[0];
//    }

    /**
     * 获取用户选中的语言设置
     *
     * @return
     */
    public static Locale getCustomLocale() {
        switch (getSpUtils().getInt(CUSTOM_LOCALE, zh_cn)) {
            case zh_cn:
                return Locale.CHINA;
            case zh_tw:
                return Locale.TAIWAN;
            case en_us:
                return Locale.ENGLISH;
            default:
                return Locale.CHINA;
        }
    }

    /**
     * 获取系统的Locale
     *
     * @return
     */
    public static Locale getSystemLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }

        return locale;
    }

    public static void doChange(Context context, int language, @Nullable Class clazz) {
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();

        if (language == CHINESE) {
            configuration.setLocale(Locale.CHINESE);
        } else if (language == ENGLISH) {
            configuration.setLocale(Locale.ENGLISH);
        }

        resources.updateConfiguration(configuration, dm);

        if (clazz != null) {
            Intent intent = new Intent(context, clazz);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
        }

//        BaseApplication.setLanguage(language);
    }

    /**
     * 重置locale
     *
     * @param context
     */
    public static void resetLanguage(Context context) {
        int lang = Utils.getSpUtils().getInt("language");
        if (lang == -1) {
            Locale locale = Utils.getSystemLocale();
            if (locale.getLanguage().equalsIgnoreCase(new Locale("zh").getLanguage())) {
                Utils.doChange(context, Utils.CHINESE, null);
            } else if (locale.getLanguage().equalsIgnoreCase(new Locale("en").getLanguage())) {
                Utils.doChange(context, Utils.ENGLISH, null);
            }
        } else {
            Utils.doChange(context, lang, null);
        }
    }

}
